/*
 * @Author: DK
 * @Date: 2024-04-27 11:26:32
 * @Last Modified by: mikey.zhaopeng
 * @Last Modified time: 2025-05-05 12:52:15
 */
import axios, { type AxiosInstance } from 'axios'
import { ElMessage } from 'element-plus'
import router from '../router'
import { getToken } from '@/utils/store'
class Base {
    instance: AxiosInstance
    constructor(baseUrl?: string) {
        const instance = axios.create()
        instance.defaults.baseURL = baseUrl || import.meta.env.SYSTEM_BASE_API
        instance.defaults.withCredentials = true
        instance.interceptors.request.use(
            (config) => {
                console.log(config)
                config.headers['satoken'] = getToken()
                return config
            },
            (error) => {
                return Promise.reject(error)
            }
        )
        instance.interceptors.response.use(
            (response) => {
                // 如果是下载
                if (response.data instanceof Blob) {
                    return response.data
                }
                // 如果返回成功
                if (response.data && response.data.code === 0) {
                    return response.data.data
                }

                if (response.data.success !== 0) {
                    switch (response.data.code) {
                        case 1:
                            const currentRoute = router.currentRoute.value
                            if (currentRoute.name !== 'Login') {
                                ElMessage.error('未登录，请重新登录')
                                router.replace({
                                    name: 'Login',
                                    query: { redirect: router.currentRoute.value.fullPath }
                                })
                            }
                            return Promise.reject(response.data.data)

                        default:
                            ElMessage.error(response.data.msg || '未知错误！')
                            return Promise.reject(response.data.data)
                    }
                }
            },
            (error) => {
                return Promise.reject(error)
            }
        )
        this.instance = instance
    }

    get(url: string, params: any = {}): Promise<any> {
        return this.instance({
            method: 'get',
            url: url,
            params: params,
            headers: {
            }
        })
    }
    post(url: string, data: any = {}): Promise<any> {
        return this.instance({
            method: 'post',
            url,
            data,
            headers: {
                'Content-Type': 'application/json;charset=utf-8',
            }
        })
    }
    patch(url: string, data: any = {}): Promise<any> {
        return this.instance({
            method: 'patch',
            url,
            data,
            headers: {
                'Content-Type': 'application/json;charset=utf-8',
            }
        })
    }
    put(url: string, data: any = {}): Promise<any> {
        return this.instance({
            method: 'put',
            url,
            data,
            headers: {
                'Content-Type': 'application/json;charset=utf-8',
            }
        })
    }
    delete(url: string, data: any = {}): Promise<any> {
        return this.instance({
            method: 'delete',
            url,
            data,
            headers: {
                'Content-Type': 'application/json;charset=utf-8',
            }
        })
    }
    download(url: string, data: any = {}): Promise<any> {
        return this.instance({
            method: 'post',
            url,
            data,
            headers: {
                'Access-Control-Allow-Credentials': 'true',
                'Content-Type': 'application/json;charset=utf-8',
            },
            responseType: 'blob' // 防止乱码
        })
    }
    formdata(url: string, file: File, data: any = {}) {
        let formdata = new FormData()
        formdata.append('file', file)
        for (const iterator of Object.keys(data)) {
            formdata.append(iterator, data[iterator])
        }
        return this.instance({
            method: 'post',
            url,
            data: formdata,
            headers: {
                'Access-Control-Allow-Credentials': 'true',
                'Content-Type': 'multipart/form-data',
            }
        })
    }
    formdataMultFile(url: string, files: any = {}, data: any = {}) {
        let formdata = new FormData()
        for (const iterator of Object.keys(files)) {
            formdata.append(iterator, files[iterator] as File)
        }
        for (const iterator of Object.keys(data)) {
            formdata.append(iterator, data[iterator])
        }
        return this.instance({
            method: 'post',
            url,
            data: formdata,
            headers: {
                'Access-Control-Allow-Credentials': 'true',
                'Content-Type': 'multipart/form-data',
            }
        })
    }
}

export default Base
